<?php

class AdminGroup extends DataExtension implements PermissionProvider {
    private static $db = array('IsAdminGroup' => 'Boolean');

	function requireDefaultRecords() {
        if(!$staffGroup = Group::get()->find('Code', 'admin')) {
            Group::create()
            ->setField('Code', 'admin')
            ->setField('IsAdminGroup', 1)
            ->setField('Title', 'Admin')
            ->setField('Sort', 1)
            ->write();
            DB::alteration_message('Staff group created', 'created');
        }
    }
	
    function onBeforeWrite(){
        if($this->owner->Code == 'administrators' && !$this->owner->ID){
            $this->owner->IsAdminGroup = 1;
        }
    }

    function updateCMSFields(FieldList $fields) {
    	$fields->insertAfter(TextField::create('Code', $this->owner->fieldLabel('Code')), 'Title');
        $fields->insertAfter(CheckboxField::create('IsAdminGroup', _t('AdminGroup.IS_ADMIN_GROUP', 'This is the admin group.')), 'ParentID');
    }
	
	function canCreate($member){
		return Permission::checkMember($member, 'EDIT_GROUPS');
	}
	
	public function providePermissions() {
		return array(
			'EDIT_GROUPS' => array(
				'name' => _t('AdminGroup.EDIT_GROUPS', 'Manage groups'),
				'category' => _t('Permissions.PERMISSIONS_CATEGORY')
			)
		);
	}
}
